package com.yiyuan.system.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.yiyuan.common.base.BaseAction;
import com.yiyuan.system.model.PriResource;
import com.yiyuan.system.model.Role;
import com.yiyuan.system.model.RolePri;
import com.yiyuan.system.service.ISystemService;

public class RoleAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	/* ************* 分页参数封装******************************************** */
	private String startrecord;
	private String endrecord;
	private String perpage;

	/* ************* form参数******************************************* */
	private String roleId;
	private String roleName;
	private String roleDesc;
	private String selPris;
	/* ************* 输出结果封装***************************************** */
	private Role role;
	private List<Role> list;
	private List<PriResource> priUsedList;
	private List<PriResource> priAllList;

	/* ************* 各功能函数******************************************* */
	// 资源引用
	@Resource
	private ISystemService systemService;

	// 进入角色主页
	public String index() {
		return "toMain";
	}

	// 保存角色
	public void save() {
		Role role = null;
		if (selPris != null && !selPris.equals("")) {
			selPris = selPris.substring(1);
		}
		String rolePris[] = selPris.split(",");

		if (this.roleId != null && !this.roleId.equals("")) {// 修改
			role = this.systemService.loadRole(Integer.valueOf(roleId));
			role.setRoleName(this.roleName);
			role.setRoleDesc(this.roleDesc);
			role.setRolePris(rolePris);
			this.systemService.updateRole(role);
		} else {// 新增
			role = new Role();
			role.setRoleName(this.roleName);
			role.setRoleDesc(this.roleDesc);
			role.setRolePris(rolePris);
			this.systemService.addRole(role);
		}
		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter().println("1");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 机构分页显示
	@SuppressWarnings("unchecked")
	public void listPage() throws IOException{
		int start = 1;
		int end = 100;
try{
		// 三个内置分页参数
		if (this.getStartrecord() != null && this.getEndrecord() != null
				&& this.getPerpage() != null) {
			start = Integer.parseInt(this.getStartrecord());
			end = Integer.parseInt(this.getEndrecord());
		}
		// 获得页数及每页记录数
		int totalRecord = systemService.getRolePageCount(this.roleName);
		list = systemService.findRoleList(start, end,this.roleName);

		// 封装输出结果
		Map map = new HashMap();
		map.put("list", list);
		map.put("totalRecord", totalRecord);
		JSONObject jso = new JSONObject(map);

		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		ServletActionContext.getResponse().setContentType(
				"text/xml; charset=utf-8");

		ServletActionContext.getResponse().getWriter().print(jso.toString());}catch(Exception e){e.printStackTrace();}
		
	}

	// 删除角色
	public void del() {
		String ret = null;
		try {
			this.systemService.deleteRole(Integer.valueOf(this.roleId));
			ret = "delete";
		} catch (Exception e) {
			ret = "error";
		}

		try {
			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().setContentType(
					"text/xml; charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(ret);
		} catch (Exception e) {

		}

	}

	// 查看角色详细
	public String view() {
		if (roleId != null && !roleId.equals("")) {
			this.role = this.systemService.loadRole(Integer.valueOf(roleId));

			priUsedList = new ArrayList<PriResource>();
			priAllList = new ArrayList<PriResource>();
			PriResource pri = null;
			RolePri rolePri = null;
			boolean flag = false;

			// 加载角色权限
			List<RolePri> curPriList = this.systemService
					.findRolePriListByRoleId(Integer.valueOf(roleId));
			// 加载全部权限列表，并且封装到两个list中
			List<PriResource> priList = this.systemService.findPriResList();
			for (int i = 0; i < priList.size(); i++) {
				pri = priList.get(i);
				for (int j = 0; j < curPriList.size(); j++) {
					rolePri = curPriList.get(j);
					if (rolePri.getPriId().intValue() == pri.getResId()
							.intValue()) {
						priUsedList.add(pri);
						flag = true;
					}
				}
				if (!flag) {
					priAllList.add(pri);
				}
				flag = false;
			}
		}
		return "toView";

	}

	// 进入角色增加页面
	public String add() {
		// 加载全部权限列表
		this.priAllList = this.systemService.findPriResList();
		return "toAdd";
	}

	/** **************************************************************** */

	public String getSelPris() {
		return selPris;
	}

	public void setSelPris(String selPris) {
		this.selPris = selPris;
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getRoleDesc() {
		return roleDesc;
	}

	public void setRoleDesc(String roleDesc) {
		this.roleDesc = roleDesc;
	}

	public String getStartrecord() {
		return startrecord;
	}

	public void setStartrecord(String startrecord) {
		this.startrecord = startrecord;
	}

	public String getEndrecord() {
		return endrecord;
	}

	public void setEndrecord(String endrecord) {
		this.endrecord = endrecord;
	}

	public String getPerpage() {
		return perpage;
	}

	public void setPerpage(String perpage) {
		this.perpage = perpage;
	}

	public List<Role> getList() {
		return list;
	}

	public Role getRole() {
		return this.role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List<PriResource> getPriUsedList() {
		return priUsedList;
	}

	public List<PriResource> getPriAllList() {
		return priAllList;
	}

}
